package com.dyh.quickdemozwei.listTra;

import com.alibaba.fastjson.JSON;
import com.dyh.quickdemozwei.t0907.T0907;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class ListTraverse<T> {


    private List consumers;


    public void addConsumer(Consumer<T> consumer){
        this.consumers.add(consumer);

    }

    public void  addConsumer(BiConsumer<T,Context> consumer){
        this.consumers.add(consumer);

    }



    public void work(List<T> datas){
        if (datas==null || datas.size()==0) return;

        Context context = new Context();

        l1:
        for (context.index=0 ;context.index<datas.size();context.index++){

            for (Object consumer : consumers) {
                if (consumer instanceof Consumer)
                    ((Consumer) consumer).accept(datas.get(context.index));
                if (consumer instanceof BiConsumer)
                    ((BiConsumer) consumer).accept(datas.get(context.index),context);

                if (context.breakSignal)
                    break l1;
                if (context.continueSignal)
                    continue l1;
            }
        }

    }


//    public static void main(String[] args) {
//        List<String> longCollect = new ArrayList<>();
//
//        ListTraverse<String> traverse = ListTraverse.Builder.init(String.class).addConsumer(
//                e->e.length()
//        ).build();
//
////        ListTraverse<String> traverse = ListTraverse.init();
//        traverse.addConsumer(e-> System.out.println(e));
//        traverse.addConsumer(e->{if (e.length()>3) longCollect.add(e);});
//        traverse.addConsumer((s, context) ->{
//            if (s.equals("1d"))
//                context.breakSignal();
//        });
//
//        List datas = new ArrayList();
//        datas.add("dawd");
//        datas.add("dawddawd");
//        datas.add("ad");
//        datas.add("1d");
//        datas.add("23");
//        datas.add("dawad");
//
//
//        traverse.work(datas);
//
//        System.out.println(longCollect);
//
//    }

    public static class Builder<T>{

      List consumers;
      ListTraverse<T> traverse;

        private Builder(ListTraverse<T> traverse) {
            this.traverse = traverse;
            this.consumers = new ArrayList<>();
        }

        public  static  <T> Builder<T> init(Class<T> tClass){
          ListTraverse<T> listTraverse = new ListTraverse<>();
          return new Builder<>(listTraverse);
        }

        public Builder<T> addConsumer(Consumer<T> consumer){
            this.consumers.add(consumer);
            return this;
        }
        public Builder addConsumer(BiConsumer<Context,T> consumer){
            this.consumers.add(consumer);
            return this;
        }

        ListTraverse<T> build(){
            traverse.consumers = this.consumers;
            return this.traverse;
        }

    }

    public static class Context{
        int index;
        boolean breakSignal;
        boolean continueSignal;


        public void breakSignal(){
            this.breakSignal = true;
        }

        public void continueSignal(){
            this.continueSignal = true;
        }


        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public boolean isBreakSignal() {
            return breakSignal;
        }


        public boolean isContinueSignal() {
            return continueSignal;
        }


    }

    public static void main(String[] args) {

        List<String> db = JSON.parseArray(
                "[\"https://p9-aio.ecombdimg.com/obj/ecom-shop-material/ALSUvYM_m_77891b75da21db58110095f0eeaea128_sx_454543_www800-800\",\"https://p3-aio.ecombdimg.com/obj/ecom-shop-material/ALSUvYM_m_57afbd40b9a25cca72bc47f8acd5aba0_sx_463373_www800-800\",\"https://p6-aio.ecombdimg.com/obj/ecom-shop-material/ALSUvYM_m_795cf228d89942f517dd107b50c5ed76_sx_503727_www800-800\",\"https://p9-aio.ecombdimg.com/obj/ecom-shop-material/ALSUvYM_m_f9c4cf3bca8d7440d56d3146c132a57a_sx_434874_www800-800\"]"
                ,String.class
        );

        String [] urls = {"url1",null,"nrl3",null,null};

        StringBuilder sb = new StringBuilder();
        for (int i=0;i<urls.length;i++){
            if (urls[i]!=null)
                sb.append(urls[i]);
            else{
                if (i<db.size()){
                    sb.append(db.get(i));
                }
            }
            if (i!=urls.length-1){
                sb.append("|");
            }
        }

        System.out.println(sb);

    }
}
